//
//  MPBNetWorkBase.h
//  MiaoPai
//
//  Created by jiaxuzhou on 2017/5/24.
//  Copyright © 2017年 jiaxuzhou. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "AFNetworking.h"
#import "MPBNetWoringSettings.h"


/**
 网络请求的底层封装。在AFNetworing之上
 */
typedef NSURLSessionTask MPURLSessionTask;


typedef BOOL(^MPHNoNetWorkBaseBlock)(void);


@protocol MPBNetWorkBase <NSObject>

/**
 设置返回的数据的解析类型

 @param responseType 解析类型
 @return 解析对象
 */
- (AFHTTPResponseSerializer *)getResponseSerializerWithResponseType:(MPE_NetResponseType)responseType;

/**
 设置静态的公共header参数
 */
- (void)setStatiCcommentHTTPHeaders;

/**
  设置动态的公共header参数

 @param request 网络请求对象
 */
- (void)setChangeCommentHTTPHeadersWithRequest:(NSMutableURLRequest *)request;

/**
 设置静态的公共body参数

 @return 公共参数
 */
- (NSMutableDictionary *)setStaticCommentHTTPBody;

/**
 创建 NSMutableURLRequest 系统请求对象

 @param method 请求方式 POST GET
 @param parameters body参数
 @param path 请求路径
 @param requestSerializer AFN请求对象
 @param baseURL baseURL
 @return 系统请求对象
 */
- (NSMutableURLRequest *)getSendHttpRequestByMentod:(MPE_NetMethod)method
                                              param:(NSDictionary *)parameters
                                               path:(NSString *)path
                            afhttpRequestSerializer:(AFHTTPRequestSerializer *)requestSerializer
                                            baseURL:(NSURL *)baseURL;
/**
 添加签名

 @param request 系统网络请求
 @param parameters body参数
 */
- (void)addSignCodeWithRequest:(NSMutableURLRequest *)request param:(NSDictionary *)parameters;

/**
 加密
 
 @param request 请求对象
 @param parameters 请求的所有参数
 */
- (void)addEncryptCodeWithRequest:(NSMutableURLRequest *)request param:(NSDictionary *)parameters;

/**
 获取缓存数据

 @param request 请求对象
 @param method 请求方式 POST GET
 @param catcheData 缓存
 */
- (void)getCacteDataWithRequest:(NSURLRequest *)request
                         method:(MPE_NetMethod)method
                     catcheData:(void (^)(id responseObject))catcheData;

/**
 发送日志

 @param request 请求对象
 @param response 返回结果对象
 @param responseObject 返回结果对象
 @param dataTask 发送网络请求的task对象
 @param error 错误信息
 @param method 请求方式 POST GET
 @param isCatche 是否需要读取缓存
 @param isSend 是否立即发送
 */
- (void)sendLogToServer:(NSURLRequest *)request
               response:(NSURLResponse *)response
         responseObject:(id)responseObject
               dataTask:(NSURLSessionTask *)dataTask
                  error:(NSError *)error
                 method:(MPE_NetMethod)method
               isCatche:(BOOL)isCatche
                 isSend:(BOOL)isSend;

/**
  检查是否有网络
 
 @param isShow 底层是否展示无网络提示
 @param noNetBlock 无网络回调
 @return 返回是否有网络 YES 有网络 NO 无网络
 */
+ (BOOL)hasNetWork:(BOOL)isShow noNetBlock:(BOOL(^)())noNetBlock;


/**
 根据请求结果分析成功失败

 @param idResult 请求结果
 @param task 发送网络请求的task
 @param isShowToastView4NetWorkState 是否展示错误信息
 @param cached 缓存回调
 @param sessionTimeoutBlock session过期回调
 @return 返回网络请求的结果状态
 */
+ (MPE_NetWorkState)checkState4Result:(id)idResult
                                 task:(MPURLSessionTask *)task
         isShowToastView4NetWorkState:(BOOL)isShowToastView4NetWorkState
                               cached:(void (^)(NSDictionary *cachedObject))cached
                  sessionTimeoutBlock:(void (^)(MPE_NetWorkSessionState state))sessionTimeoutBlock;

/**
 解析response外层数据

 @param resp 网络数据源
 @return 解析后的数据
 */
+ (id)resetResponse:(id)resp;


@end


@interface MPBNetWorkBase : NSObject <MPBNetWorkBase>

/// 没有网络回调 BOOL 返回YES 代表需要底层展示无网络提示 NO 不需要底层展示
@property (nonatomic, copy) BOOL (^noNetWorkInBaseBlock)(void);

/// 新旧api替换
@property (nonatomic, assign) BOOL oldApi;
/// response加密方式
@property (nonatomic, assign) MPE_NetRespSecType responseSecType;
@property (nonatomic, assign) BOOL reqByTotalUrl;

- (instancetype)initWithApi:(BOOL)isOld;
- (void)setSessionManager:(AFHTTPSessionManager *)manager;

/**
 通过 NSURLSessionConfiguration 对象初始化AFHTTPSessionManager 个别的需要设置configuration

 @param configuration NSURLSessionConfiguration
 @return 返回 MPBNetWorkBase对象
 */
- (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)configuration;

/**
 GET

 @param URLString path
 @param parameters 参数
 @param isShowNoNetAlert 是否展示无网络提示
 @param success 成功回调
 @param failure 失败回调
 @param cached 读取缓存回调
 @return MPURLSessionTask
 */
- (MPURLSessionTask *)GET:(NSString *)URLString
               parameters:(id)parameters
         isShowNoNetAlert:(BOOL)isShowNoNetAlert
                isEncrypt:(BOOL)isEncrypt
                 priority:(MPE_NetWorkPriority)priority
                  success:(void (^)(MPURLSessionTask *operation, id responseObject))success
                  failure:(void (^)(MPURLSessionTask *operation, NSError *error))failure
                   cached:(void (^)(NSDictionary *cachedObject))cached;

- (MPURLSessionTask *)GET:(NSString *)URLString
               parameters:(id)parameters
         isShowNoNetAlert:(BOOL)isShowNoNetAlert
                isEncrypt:(BOOL)isEncrypt
                 priority:(MPE_NetWorkPriority)priority
                  success:(void (^)(MPURLSessionTask *operation, id responseObject))success
                  failure:(void (^)(MPURLSessionTask *operation, NSError *error))failure
                noNetWork:(MPHNoNetWorkBaseBlock)noNetWork
                   cached:(void (^)(NSDictionary *cachedObject))cached;
/**
 POST 普通
 
 @param URLString path
 @param parameters 参数
 @param isShowNoNetAlert 是否展示无网络提示
 @param success 成功回调
 @param failure 失败回调
 @param cached 读取缓存回调
 @return MPURLSessionTask
 */
- (MPURLSessionTask *)POST:(NSString *)URLString
                parameters:(id)parameters
          isShowNoNetAlert:(BOOL)isShowNoNetAlert
                 isEncrypt:(BOOL)isEncrypt
                  priority:(MPE_NetWorkPriority)priority
                   success:(void (^)(MPURLSessionTask *operation, id responseObject))success
                   failure:(void (^)(MPURLSessionTask *operation, NSError *error))failure
                    cached:(void (^)(NSDictionary *cachedObject))cached;

- (MPURLSessionTask *)POST:(NSString *)URLString
                parameters:(id)parameters
          isShowNoNetAlert:(BOOL)isShowNoNetAlert
                 isEncrypt:(BOOL)isEncrypt
                  priority:(MPE_NetWorkPriority)priority
                   success:(void (^)(MPURLSessionTask *operation, id responseObject))success
                   failure:(void (^)(MPURLSessionTask *operation, NSError *error))failure
                 noNetWork:(MPHNoNetWorkBaseBlock)noNetWork
                    cached:(void (^)(NSDictionary *cachedObject))cached;
/**
 POST 上传文件

 @param URLString path
 @param parameters 参数
 @param isShowNoNetAlert 是否展示无网络提示
 @param isEncrypt 参数是否加密
 @param priority 网络优先级
 @param block 文件流获取
 @param success 成功回调
 @param failure 失败回调
 @return 返回发送请求的Task
 */
- (MPURLSessionTask *)POST:(NSString *)URLString
                parameters:(id)parameters
          isShowNoNetAlert:(BOOL)isShowNoNetAlert
                 isEncrypt:(BOOL)isEncrypt
                  priority:(MPE_NetWorkPriority)priority
 constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
                   success:(void (^)(MPURLSessionTask *operation, id responseObject))success
                   failure:(void (^)(MPURLSessionTask *operation, NSError *error))failure;
/**
  同步网络请求--简单参数

 @param URLString path
 @param parameters 参数
 @param isShowNoNetAlert 是否展示无网络提示
 @param isEncrypt 参数是否加密
 @param priority 网络优先级
 @param cached 缓存回调
 @return 返回 请求结果数据
 */
- (id)netWorkSyncWithURL:(NSString *)URLString
                  params:(NSDictionary *)parameters
        isShowNoNetAlert:(BOOL)isShowNoNetAlert
               isEncrypt:(BOOL)isEncrypt
                priority:(MPE_NetWorkPriority)priority
                  cached:(void (^)(NSDictionary *cachedObject))cached;

/**
  同步网络请求 -- 失败成功回调

 @param URLString path
 @param parameters 参数
 @param isShowNoNetAlert 是否展示无网络提示
 @param isEncrypt 参数是否加密
 @param priority 网络优先级
 @param success 请求成功回调
 @param failure 请求失败回调
 @param cached 缓存回调
 @return 返回请求结果数据
 */
- (id)netWorkSyncWithURL:(NSString *)URLString
                  params:(NSDictionary *)parameters
        isShowNoNetAlert:(BOOL)isShowNoNetAlert
               isEncrypt:(BOOL)isEncrypt
                priority:(MPE_NetWorkPriority)priority
                 success:(void (^)(id responseObject))success
                 failure:(void (^)(id responseObject))failure
                  cached:(void (^)(NSDictionary *cachedObject))cached;

/**
  同步网络请求，带method参数

 @param method 请求方式。POST GET
 @param URLString path
 @param parameters 参数
 @param isShowNoNetAlert 是否展示无网络提示
 @param isEncrypt 参数是否加密
 @param priority 网络优先级
 @param success 请求成功回调
 @param failure 请求失败回调
 @param cached 缓存回调
 @return 返回请求结果数据
 */
- (id)netWorkSyncWithMethod:(NSString *)method
                        URL:(NSString *)URLString
                     params:(NSDictionary *)parameters
           isShowNoNetAlert:(BOOL)isShowNoNetAlert
           isEncrypt:(BOOL)isEncrypt
                   priority:(MPE_NetWorkPriority)priority
                    success:(void (^)(id responseObject))success
                    failure:(void (^)(id responseObject))failure
                     cached:(void (^)(NSDictionary *cachedObject))cached;

/**
 以下方法是用于MPBNetWorking子类传入请求数据
 */

/**
 设置path

 @param strURL path
 */
- (void)setRequestURL:(NSString *)strURL;

/**
 设置参数

 @param dictParams 参数
 */
- (void)setOverloadParameters:(NSDictionary *)dictParams;

/**
 设置header公共参数

 @param dicHeaderParams header公共参数
 */
- (void)setOverloadHeaderCommentParameters:(NSDictionary *)dicHeaderParams;

/**
 设置body公共参数

 @param dicBodyParams body公共参数
 */
- (void)setOverloadBodyCommentParameters:(NSDictionary *)dicBodyParams;

/**
 设置网络请求返回数据类型

 @param type  网络请求返回数据类型
 */
- (void)setResponseType:(MPE_NetResponseType)type;

/**
 设置超时时间

 @param timeoutInterval 超时时间
 */
- (void)setTimeoutInterval:(CGFloat)timeoutInterval;

/**
 取消一个对象下的所有网络请求
 */
+ (void)cancelAllRequest;

/**
 通过URL取消网络请求

 @param url url
 */
+ (void)cancelRequestWithURL:(NSString *)url;

/**
 获取所有网络请求数量

 @return 所有网络请求数量
 */
+ (NSUInteger)tasksCount;

/**
 获取所有网络请求对象数组

 @return 所有网络请求对象数组
 */
+ (NSMutableArray *)allTasks;

/**
 获取AFN发送网络请求的管理对象

 @return AFN发送网络请求的管理对象
 */
- (AFHTTPSessionManager *)getHttpSessionManager;

/**
 处理失败成功逻辑

 @param idResult 返回结果数据
 @param task 发送网络的task
 @param isShowToastView4NetWorkState 是否展示错误提示
 @param cached 缓存回调
 @param sessionTimeoutBlock session过期回调
 @return 网络请求结果状态
 */
+ (MPE_NetWorkState)checkState4Result:(id)idResult
                                 task:(MPURLSessionTask *)task
         isShowToastView4NetWorkState:(BOOL)isShowToastView4NetWorkState
                               cached:(void (^)(NSDictionary *cachedObject))cached
                  sessionTimeoutBlock:(void (^)(MPE_NetWorkSessionState state))sessionTimeoutBlock;


@end
